const state = {
    // 文章数据
    articleData:[
        {
            id:1,
            title:'我的之日1',
            content:'是一个小dsajkdhagiuasgisagfoasifgasocxnmcbasjqjdgigaiudgadasgduis故事',
            comment:''
        },
    ],
    searchKey:'',//搜索关键词
    selectedId:'',//选中的文章id
};

const mutations = {
    // 搜索文章
    searchArticle(state, payload){
        state.searchKey=payload
    },

    // 选中文章
    setSelectedId(state, payload){
        state.selectedId=payload
    },

    // 删除文章：id
    deleteArticle(state, payload){
        let idx=state.articleData.findIndex(item=>item.id==payload)
        state.articleData.splice(idx,1)
    },

    // 创建文章：标题,内容,id
    createArticle(state, payload){
        state.articleData.push(payload)
    },

    // 编辑文章：标题,内容
    updateArticle(state, payload){
        let idx=state.articleData.findIndex(item=>item.id==state.selectedId)
        state.articleData[idx].title=payload.title
        state.articleData[idx].content=payload.content
    },
};

const getters = {
    // 获取选中的文章
    getArticle(state){
        return state.articleData.find(item=>item.id==state.selectedId)
    },

    // 检索后的文章
    searchArticleData(state) {
        if(state.searchKey==''){
            return state.articleData
        }
        return state.articleData.filter(item=>item.title==state.searchKey)
    },
};

export default {
  state,
  getters,
  mutations
};
